﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Pages
{
    public partial class UserActive : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.IsVerified)
            {
                lbLoginStatus.Text = "已登录";
            }
            if (Request["userid"] != null && Request["key"] != null)
            {
                string userid = Request["userid"];
                Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(Request["userid"]);
                if (user != null)
                {
                    lbActiveUser.Text = user.UserName;

                    if (user.State == (int)Common.Enum.auvState.Enabled)
                    {
                        lbMessage.Text = "用户已激活";
                        ShowCommand(true);
                    }
                    else
                    {
                        if (BLL.UserInfoBLL.Instance.Activate(Request["userid"], Request["key"]))
                        {
                            //激活完成后自动登录

                            lbMessage.Text = "激活成功！";
                            ShowCommand(true);
                        }
                        else
                        {
                            lbMessage.Text = "激活链接已失效！";
                            ShowCommand(false);
                        }
                    }
                }
                else
                {
                    lbActiveUser.Text = "用户不存在！";
                    ShowCommand(false);
                }
            }
            else
            {
                Response.Redirect(URL_INDEX);
            }
        }

        private void ShowCommand(bool success)
        {
            if (success)
            {
                if (base.IsVerified && base.VerifiedUserID == Request["userid"])
                {
                    Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(Request["userid"]);
                    base.VerifiedInfo = new AUV5.Model.VerifiedInfo(user);
                    lbtnIndex.Visible = true;
                    base.SuccessInfo = "激活成功！";
                    lbtnIndex.PostBackUrl = URL_INDEX;
                }
                else
                {
                    lbtnLogin.Visible = true;
                    lbtnLogin.PostBackUrl = URL_LOGIN;
                }
            }
            else
            {
                if (base.IsVerified)
                {
                    lbtnSendActiveEmail.Visible = true;
                    lbtnSendActiveEmail.PostBackUrl = "UserSendActiveEmail.aspx";
                }
                else
                {
                    lbtnLogin.Visible = true;
                    lbtnLogin.Text = "点此登录后重新发送激活邮件";
                    lbtnLogin.PostBackUrl = URL_LOGIN;
                }
            }
        }
    }
}